package com.zbkj.service.service.monitor.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zbkj.common.model.monitor.ZfbMonitorEquipment;
import com.zbkj.common.monitorchannel.vo.hemengyun.HeMengYunCallBackDeviceStatusNoticeVo;
import com.zbkj.common.monitorchannel.vo.hemengyun.HeMengYunResultVo;
import com.zbkj.service.dao.monitor.ZfbMonitorEquipmentMapper;
import com.zbkj.service.service.monitor.IHeMengYunCallBackService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 设备状态回调通知处理
 */
@Service
@Slf4j
public class HeMengYunDeviceStatusNoticeCallBackServiceImpl implements IHeMengYunCallBackService {

    @Autowired
    private ZfbMonitorEquipmentMapper zfbMonitorEquipmentMapper;

    @Override
    public void callBack(String action, String json) {
        if (!Objects.equals(action, "DeviceStatusNotice")) {
            return;
        }

        HeMengYunResultVo<HeMengYunCallBackDeviceStatusNoticeVo> resultVo = JSON.parseObject(json, new TypeReference<HeMengYunResultVo<HeMengYunCallBackDeviceStatusNoticeVo>>() {
        });

        HeMengYunCallBackDeviceStatusNoticeVo data = resultVo.getData();

        if (ObjectUtil.isNull(data)) {
            log.info("DeviceStatusNotice 回调数据为空={}", json);
            return;
        }

        String license = data.getLicense();
        if (StringUtils.isBlank(license)) {
            log.info("DeviceStatusNotice 回调数据中设备的license为空={}", json);
            return;
        }

        ZfbMonitorEquipment monitorEquipment = zfbMonitorEquipmentMapper.selectOne(new LambdaQueryWrapper<ZfbMonitorEquipment>()
                .eq(ZfbMonitorEquipment::getLicense, license).last("limit 1"));

        if (ObjectUtil.isNull(monitorEquipment) || ObjectUtil.isNull(monitorEquipment.getId())) {
            log.info("license={}的设备，在系统中未找到", license);
            return;
        }

        // 设备状态; 0:未知; 1: 在线; 2:离线; 3:休眠; 6:设备区域限制; 7:4G 卡限制
        String status = data.getStatus();
        Integer oldStatus = monitorEquipment.getOnlineStatus();
        if (!Objects.equals(oldStatus, status)) {
            monitorEquipment.setOnlineStatus(Integer.parseInt(status));
            monitorEquipment.setUpdateTime(LocalDateTime.now());
            if (monitorEquipment.getOnlineStatus().equals(1)) {
                monitorEquipment.setLastOnlineTime(monitorEquipment.getUpdateTime());
            }
            zfbMonitorEquipmentMapper.updateById(monitorEquipment);
//            log.info("DeviceStatusNotice 成功更新设备={}的在线状态，系统中原状态={}，通知过来的新状态={}", license, oldStatus, status);
//        } else {
//            log.info("DeviceStatusNotice 设备={}的在线状态未发生变化，无需更新，系统中原状态={}，通知过来的新状态={}", license, oldStatus, status);
        }
    }
}
